package com.tddx.activiti.utils.ftl;

import java.io.File;

import org.apache.log4j.Logger;

import com.tddx.activiti.utils.ftl.data.BaseData;

public class FtlUtil {
	
	private static Logger log = Logger.getLogger(FtlUtil.class);
	
	public final static String fseperator = "/";
	
	/**
	 * 模版文件存放位置
	 * 相对路径
	 *
	 */
	private static String FROM_FILE = File.separator + "com" + File.separator + "tddx" + File.separator + "activiti" + File.separator + "template" + File.separator + "genti";
	
	/**
	 * 生成文件的存放位置
	 * 绝对路径
	 * 
	 */
	private static String TO_JAVA_FILE = "F:" + File.separator + "shop" + File.separator + "activiti" + File.separator + "jeebmst" + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + "com" + File.separator + "tddx" + File.separator + "activiti" + File.separator + "business";
	
	/**
	 * 生成文件的存放位置
	 * 绝对路径
	 * 
	 */
	private static String TO_JSP_FILE = "F:" + File.separator + "shop" + File.separator + "activiti" + File.separator + "jeebmst" + File.separator + "src" + File.separator + "main" + File.separator + "webapp" + File.separator + "WEB-INF" + File.separator + "views" + File.separator + "modules";
	
	
	public static void makeFtl(String ftlName, String toFile, BaseData data, boolean isPathAbsolute){
		
		FtlMarker maker = null;
		if(isPathAbsolute){//绝对路径走下面流程;
			maker = new FtlMarker(FROM_FILE, toFile, ftlName);
		}else if(toFile.endsWith(".java")){//java放到java路径 TO_JAVA_FILE
			maker = new FtlMarker(FROM_FILE, TO_JAVA_FILE + File.separator + toFile, ftlName);
		}else{//jsp放到jsp路径放TO_JSP_FILE
			maker = new FtlMarker(FROM_FILE, TO_JSP_FILE + File.separator + toFile, ftlName);
		}
		try {
			maker.initTemplate(data).writeFile();
		} catch (Exception e) {
			log.error("生成文件：" + ftlName + "失败！");
			e.printStackTrace();
		}
	}
	
	public static void makeFtl(String ftlName, String toFile, BaseData data){
		makeFtl(ftlName, toFile, data, false);
	}
	
	public static void makeControllerFtl(BaseData data){
		makeFtl("controller.ftl", data.getPath().replace(FtlUtil.fseperator, FtlUtil.fseperator + "controller" + FtlUtil.fseperator) + File.separator + data.getName() + "Ctrl.java", data);
	}
	
	public static void makeServiceFtl(BaseData data){
		makeFtl("service.ftl", data.getPath().replace(FtlUtil.fseperator, FtlUtil.fseperator + "service" + FtlUtil.fseperator) + File.separator + data.getName() + "Service.java", data);
	}
	
	public static void makeDaoFtl(BaseData data){
		makeFtl("dao.ftl", data.getPath().replace(FtlUtil.fseperator, FtlUtil.fseperator + "dao" + FtlUtil.fseperator) + File.separator + data.getName() + "Dao.java", data);
	}
	
	public static void makeJdbcFtl(BaseData data){
		makeFtl("jdbc.ftl", data.getPath().replace(FtlUtil.fseperator, FtlUtil.fseperator + "dao" + FtlUtil.fseperator) + File.separator + data.getName() + "Jdbc.java", data);
	}
	
	public static void makeEntityFtl(BaseData data){
		makeFtl("entity.ftl", data.getPath().replace(FtlUtil.fseperator, FtlUtil.fseperator + "entity" + FtlUtil.fseperator) + File.separator + data.getName() + "Entity.java", data);
	}
	
	public static void makeInputFtl(BaseData data){
		makeFtl("input.ftl", data.getMinpckage() + File.separator + data.getMaxpckage() + "Input.jsp", data);
	}
	
	public static void makeResultFtl(BaseData data){
		makeFtl("list.ftl", data.getMinpckage() + File.separator + data.getMaxpckage() + "List.jsp", data);
	}
	
	public static void makeTreeResultFtl(BaseData data){
		makeFtl("tree.ftl", data.getMinpckage() + File.separator + data.getMaxpckage() + "Tree.jsp", data);
	}
	
	public static void makeTreeInputFtl(BaseData data){
		makeFtl("input.ftl", data.getMinpckage() + File.separator + data.getMaxpckage() + "Input.jsp", data);
	}
	
}
